<!DOCTYPE html>
<html lang="ja">
  <head>
    <meta charset="UTF-8" />
    <title>Answer</title>
    <script
      crossorigin
      src="https://unpkg.com/react@16/umd/react.development.js"
    ></script>
    <script
      crossorigin
      src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"
    ></script>
    <script
      crossorigin
      src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.34/browser.min.js"
    ></script>
    <script src="https://cdn.jsdelivr.net/npm/babel-regenerator-runtime@6.5.0/runtime.min.js"></script>
  </head>
  <body>
    <div class="main">
      <div class="section" id="app1"></div>
    </div>
    <script type="text/babel">
      const App = () => {
        const remoteRef = React.useRef();
        const localRef = React.useRef();

        React.useEffect(() => {
          (async () => {
            const socket = new WebSocket("ws://192.168.0.101:8888");
            await new Promise((r) => (socket.onopen = r));
            console.log("open websocket");

            const sender = new RTCPeerConnection({
              iceServers: [],
            });
            sender.onicecandidate = ({ candidate }) => {
              if (!candidate) {
                const sdp = JSON.stringify(sender.localDescription);
                socket.send(sdp);
              }
            };
            const receiver = new RTCPeerConnection({
              iceServers: [],
            });
            receiver.ontrack = (e) => {
              remoteRef.current.srcObject = e.streams[0];
            };
            receiver.onicecandidate = ({ candidate }) => {
              if (!candidate) {
                const sdp = JSON.stringify(receiver.localDescription);
                socket.send(sdp);
              }
            };

            {
              const offer = await new Promise(
                (r) => (socket.onmessage = (ev) => r(JSON.parse(ev.data)))
              );
              console.log(offer.sdp);
              const localStream = await navigator.mediaDevices.getUserMedia({
                video: { width: 1920 },
              });
              localRef.current.srcObject = localStream;
              localStream.getVideoTracks().forEach((track) => {
                const t = sender.addTrack(track, localStream);
                setTimeout(() => {
                  const params = t.getParameters();
                  params.encodings = [{ maxBitrate: 45 * 1000 * 1000 }];
                  t.setParameters(params);
                }, 1000);
              });
              await sender.setRemoteDescription(offer);
              const answer = await sender.createAnswer();
              await sender.setLocalDescription(answer);
            }
            {
              const offer = await new Promise(
                (r) => (socket.onmessage = (ev) => r(JSON.parse(ev.data)))
              );
              console.log(offer.sdp);
              await receiver.setRemoteDescription(offer);
              const answer = await receiver.createAnswer();
              await receiver.setLocalDescription(answer);
            }
          })();
        }, []);

        return (
          <div>
            answer
            <div style={{ display: "flex" }}>
              <div>
                <p>local</p>
                <video
                  ref={localRef}
                  autoPlay
                  muted
                  style={{ maxWidth: 500 }}
                />
              </div>
              <div>
                <p>remote</p>
                <video
                  ref={remoteRef}
                  autoPlay
                  muted
                  style={{ maxWidth: 500 }}
                />
              </div>
            </div>
          </div>
        );
      };

      ReactDOM.render(<App />, document.getElementById("app1"));
    </script>
  </body>
</html>
